let intent = $engines.myEngine().execArgv.intent;
if (!intent) {
    console.log("非监听启动");
    exit();
}

let fileName = intent.extras.getString("fileName");


if (!device.isScreenOn()) {
    device.keepScreenOn(30 * 1000);
    sleep(500);
    swipe(600, 900, 100, 900, 320);
    sleep(500);
}

device.keepScreenOn(30 * 1000);
sleep(10 * 1000);

sendmaila(fileName);

function sendmaila(filepath) {
    app.sendEmail({
        email: ["1049725878@qq.com"],
        subject: "这是一个邮件标题",
        text: filepath + ' .'
    });
    

    sleep(2000);
    click(101, 800);
    sleep(2000);

    if(id("add_attachment_btn").exists()){
        selectFile(filepath);
        sleep(2000);
    }

    click(654, 110);
    sleep(500);
}

function selectFile(filepath) {
    if ("" == filepath)
        return;
    id("add_attachment_btn").findOne().click();
    sleep(2000);
    click(598, 837);
    sleep(1000);

    findAndClick("内部存储");
    findAndClick("脚本");
    findAndClick("zddaka");
    findAndClick("截图");
    findAndClick(filepath);
    id("icon2").findOne().click()
    sleep(1000);
}


function findAndClick(context) {
    for (var i = 0; i < 20; i++) {
        var p = text(context);
        if (p.exists()) {
            var k = p.findOne().bounds();
            click(k.centerX(), k.centerY());
            sleep(500);
            return;
        }
        id("list").findOne().scrollForward();
        sleep(500);
    }
}


